﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Continuum.Classes;

namespace Continuum
{
    public partial class GuidedTransaction : Form
    {
        public GuidedTransaction()
        {
            InitializeComponent();
        }

        private void GuidedTransaction_Load(object sender, EventArgs e)
        {
            ddlFeederFund.Items.Add("");

            foreach (Fund fund in MainForm.projector.Funds)
            {
                ddlPrimaryFund.Items.Add(fund.Name);
                ddlFeederFund.Items.Add(fund.Name);
            }
        }

        private void ClearForm()
        {
            txtActorName.Text = "Sample Transaction";
            txtTransactionAmount.Text = "0.00";
        }

        private bool ValidateForm()
        {
            string ErrorList = "";

            txtActorName.Text = txtActorName.Text.Trim();
            if (txtActorName.Text == "")
                ErrorList += "- You must enter a valid Actor Name\n";
            else
            {
                if (MainForm.projector.FindActor(txtActorName.Text) != null)
                    ErrorList += "- An actor by that name already exists!\n";
            }

            if (ddlPrimaryFund.SelectedItem == null)
                ErrorList += "- You must select a valid Fund to post to\n";

            try
            {
                Decimal TransactionAmount = Decimal.Parse(txtTransactionAmount.Text);
            }
            catch { ErrorList += "- You must enter a valid Transaction Amount\n"; }

            if (ErrorList == "") return true;

            MessageBox.Show("Please correct the following errors :\n" + ErrorList, "Unable to save transaction", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }

        #region Button Handlers

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateForm()) return;

            Decimal TransactionAmount = Decimal.Parse(txtTransactionAmount.Text);

            Actor actor = null;

            if (ddlFeederFund.Text != "")
            {
                actor = new Actor(txtActorName.Text, ddlPrimaryFund.Text, ActorPeriodicityEnum.OnceOnly, 1, dtpTransactionDate.Value.Date, TransactionAmount); 
            }
            else
            {
                actor = new Actor(txtActorName.Text, ddlPrimaryFund.Text, ddlFeederFund.Text, ActorPeriodicityEnum.OnceOnly, 1, dtpTransactionDate.Value.Date, TransactionAmount);
            }

            MainForm.projector.Actors.Add(actor);

            MainForm.InstanceRef.HasChanged = true;

            if (chkLeaveOpen.Checked) ClearForm();
            else Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion

        #region Simple Numeric Textbox Filtering

        private void txtTransactionAmount_KeyDown(object sender, KeyEventArgs e)
        {
            if (!(e.KeyValue >= '0' && e.KeyValue <= '9') && !(e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
                && e.KeyCode != Keys.Decimal && e.KeyCode != Keys.Back && e.KeyCode != Keys.OemPeriod
                && e.KeyCode != Keys.Delete && e.KeyCode != Keys.Insert && e.KeyCode != Keys.OemMinus && e.KeyCode != Keys.Subtract
                && e.KeyCode != Keys.Left && e.KeyCode != Keys.Right
               ) e.SuppressKeyPress = true;
        }

        #endregion

        private void lnkOnlineHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.UseShellExecute = true;
            proc.StartInfo.FileName = "http://project-continuum.com/OnlineHelp/GuidedTransactions.aspx";
            proc.Start();
        }

    }
}
